package com.nandi.emergencyinspection.ui.activity.macro


import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.nandi.emergencyinspection.R
import com.nandi.emergencyinspection.bean.MacroInfoListBean
import com.nandi.emergencyinspection.mvp.macro.MacroLookListContract
import com.nandi.emergencyinspection.mvp.macro.MacrolookListPresenter
import com.nandi.emergencyinspection.ui.activity.TimeSelectActivity
import com.nandi.emergencyinspection.ui.adapter.MacroLookListAdapter
import com.nandi.emergencyinspection.utils.Random
import com.tbright.ktbaselibrary.extension.isVisiable
import com.tbright.ktbaselibrary.mvp.BaseMvpActivity
import kotlinx.android.synthetic.main.activity_main_macro.*
import kotlinx.android.synthetic.main.header_layout.*
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList


class MacroLookListActivity : BaseMvpActivity<MacrolookListPresenter>(),
    MacroLookListContract.MacroLookListView {
    private var list: MutableList<MacroInfoListBean.RecordsBean?> = ArrayList()
    val marcoSaveAdapter = MacroLookListAdapter(list)
    var page = 1
    var intExtra: Int = 0
    private var size = 10
    override fun getLayoutId(): Int {
        return R.layout.activity_main_macro
    }

    override fun initView(savedInstanceState: Bundle?) {
        center_txt.text = "巡查详情列表"
        left_icon.setOnClickListener {
            finish()
        }
        right_icon.setImageResource(R.drawable.ic_calendar)
        right_icon.isVisiable = true
        right_icon.setOnClickListener {
            startActivityForResult(
                Intent(this, TimeSelectActivity::class.java), 0x12
            )
        }
        intExtra = intent.getIntExtra("disastrerID", 0)
        dataShow.apply {
            layoutManager = LinearLayoutManager(this@MacroLookListActivity)
            adapter = marcoSaveAdapter
        }
        marcoSaveAdapter.setEmptyView(R.layout.empty_item)
        marcoSaveAdapter.setList(list)
        marcoSaveAdapter.setOnItemClickListener { adapter, view, position ->
            val any = adapter.data[position] as MacroInfoListBean.RecordsBean
            startActivity(
                Intent(this, MacroInfoActivity::class.java).putExtra(
                    "bean", any
                )
            )
        }
        initRefreshLayout()
        initLoadMore()
        mPresenter?.getList(page, size, intExtra)
    }

    /**
     * 刷新
     */
    private fun initRefreshLayout() {
        swipe.setColorSchemeColors(Color.rgb(47, 223, 189))
        swipe.setOnRefreshListener { // 这里的作用是防止下拉刷新的时候还可以上拉加载
            marcoSaveAdapter.loadMoreModule.isEnableLoadMore = false
            // 下拉刷新，需要重置页数
            page = 1
            //请求数据
            mPresenter?.getList(page, size, intExtra)
        }
    }

    /**
     * 初始化上拉加载
     */
    private fun initLoadMore() {
        marcoSaveAdapter.loadMoreModule.setOnLoadMoreListener {
            swipe.isRefreshing = false
            marcoSaveAdapter.loadMoreModule.isEnableLoadMore = true
            mPresenter?.getList(page, size, intExtra)
        }
        marcoSaveAdapter.loadMoreModule.isAutoLoadMore = true
        //当自动加载开启，同时数据不满一屏时，是否继续执行自动加载更多(默认为true)
        marcoSaveAdapter.loadMoreModule.isEnableLoadMoreIfNotFullPage = false
    }

    /**
     * 设置数据到适配器
     */
    private fun setData(data: List<MacroInfoListBean.RecordsBean>?) {
        //省略网络请求数据......
        //网络请求成功后下拉刷新与上拉加载都做如下处理:

        swipe.isRefreshing = false
        marcoSaveAdapter.loadMoreModule.isEnableLoadMore = true
        if (1 === page) {
            //如果是加载的第一页数据，用 setData()
            marcoSaveAdapter.setList(data)
        } else {
            //不是第一页，则用add
            if (data != null) {
                marcoSaveAdapter.addData(data)
            }

        }
        var dataSize = 0
        if (data != null) {
            dataSize = data.size
        }
        if (dataSize < size) {
            //如果不够一页,显示没有更多数据布局
            marcoSaveAdapter.loadMoreModule.loadMoreEnd()
        } else {
            marcoSaveAdapter.loadMoreModule.loadMoreComplete()
        }
        // page加一
        ++page
    }

    override fun showResult(result: MacroInfoListBean?) {
        setData(result?.records)
    }

    fun getCurrentTime(): String {

        val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")// HH:mm:ss
        //获取当前时间
        val date = Date(System.currentTimeMillis())
        return simpleDateFormat.format(date)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 0x12 && resultCode == 0x11) {
            val startTime = data?.getStringExtra("startTime")
            var endTime = data?.getStringExtra("endTime")
            page = 1
            when (Random.timeCompare(startTime, endTime)) {
                1, 2 -> {
                    endTime = getCurrentTime()
                }
            }
            mPresenter?.getList(
                page,
                10000,
                intExtra,
                startTime,
                endTime
            )
        }
    }
}